package com.heisenhub.uua.config;

import com.heisenhub.uua.hander.HubAuthenticationFailureHandler;
import com.heisenhub.uua.hander.HubOnAuthenticationSuccesHandler;
import com.heisenhub.uua.service.impl.UuaUserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.core.annotation.Order;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * 用于处理/oauth开头的请求，Spring Cloud OAuth内部定义的获取令牌，
 * 刷新令牌的请求地址都是以/oauth/开头的，
 * 也就是说FebsSecurityConfigure用于处理和令牌相关的请求；
 */
@Order(2)  //。在Spring中，数字越小，优先级越高
@EnableWebSecurity  //开启Security支持
public class HubSecurityConfigure  extends WebSecurityConfigurerAdapter {

    private static final String loginUrl = "/login";
    private static final String loginPage = "/authcation/login";

    /**
     * 忽略的url
     */
    private static final String[] AUTH_WHITELIST = {
            loginUrl,
            loginPage,
            "/oauth/**",
            "/login*",
            "/images/**"
    };

    @Autowired
    private UuaUserServiceImpl uuaUserService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
//        http.requestMatchers()
//                .antMatchers("/oauth/**")
//                .and()
//                .authorizeRequests()
//                .antMatchers("/oauth/**").authenticated()
//                .and().formLogin()
//                .and()
//                .csrf().disable();
//
//
//                http.csrf().disable()
//                .authorizeRequests()
//                .antMatchers("/login*").permitAll()
//                .anyRequest().authenticated()
//                .and()
//                .formLogin()
//                .and()
//                .csrf().disable(); //打开的csrf保护


        http
                // http security 要拦截的url，这里这拦截，oauth2相关和登录登录相关的url，其他的交给资源服务处理
                .requestMatchers()
                .antMatchers( AUTH_WHITELIST)
                .and()
                .authorizeRequests()
                // 自定义页面或处理url是，如果不配置全局允许，浏览器会提示服务器将页面转发多次
                .antMatchers(AUTH_WHITELIST)
                .permitAll()
                .anyRequest()
                .authenticated()
                .and()
                .formLogin()
                .and()
                .csrf().disable(); //打开的csrf保护

        // 表单登录
        http.formLogin()
                // 登录页面
                .loginPage(loginPage)
                // 登录处理url
                .loginProcessingUrl(loginUrl)
                .successHandler(new HubOnAuthenticationSuccesHandler())
                .failureHandler(new HubAuthenticationFailureHandler())
        ;
        http.httpBasic().disable();



    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(uuaUserService).passwordEncoder(passwordEncoder);
    }


}
